Events.py

Get Camera Event(s)

It shows how to configure camera event features, register an event callback, and process the events acquired from the event callback. Event example: ExposureEnd.

1 # -- coding: utf-8 --
2 
3 import sys
4 import copy
5 import platform
6 import os
7 
8 from ctypes import *
9 
10 
11 # Compatible with different operating systems to load DDL
12 currentsystem = platform.system()
13 if currentsystem == 'Windows':
14  sys.path.append(os.path.join(os.getenv('MVCAM_COMMON_RUNENV'), "Samples", "Python", "MvImport"))
15 else:
16  sys.path.append(os.path.join("..", "..", "MvImport"))
17 from MvCameraControl_class import *
18 
19 # Compatible with input processing of Python 2.X and 3.X
20 if sys.version_info[0] < 3:
21  # Python 2.x
22  input_func = raw_input
23 else:
24  # Python 3.x
25  input_func = input
26 
27 # Decoding Characters
28 def decoding_char(ctypes_char_array):
29  """
30  Safely decode a string from a ctypes character array.
31  Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments.
32  """
33  byte_str = memoryview(ctypes_char_array).tobytes()
34 
35  # Truncate at the first null character
36  null_index = byte_str.find(b'\x00')
37  if null_index != -1:
38  byte_str = byte_str[:null_index]
39 
40  # Attempt to decode using multiple encodings
41  for encoding in ['gbk', 'utf-8', 'latin-1']:
42  try:
43  return byte_str.decode(encoding)
44  except UnicodeDecodeError:
45  continue
46 
47  # If all encodings fail, use a replacement strategy
48  return byte_str.decode('latin-1', errors='replace')
49 
50 
51 fun_ctype = get_platform_functype()
52 stEventInfo = POINTER(MV_EVENT_OUT_INFO)
53 EventInfoCallBack = fun_ctype(None, stEventInfo, c_void_p)
54 
55 def event_callback(pEventInfo, pUser):
56  stPEventInfo = cast(pEventInfo, POINTER(MV_EVENT_OUT_INFO)).contents
57  nBlockId = stPEventInfo.nBlockIdHigh
58  nBlockId = (nBlockId << 32) + stPEventInfo.nBlockIdLow
59  nTimestamp = stPEventInfo.nTimestampHigh
60  nTimestamp = (nTimestamp << 32) + stPEventInfo.nTimestampLow
61  if stPEventInfo:
62  print ("EventName[%s], EventId[%u], BlockId[%d], Timestamp[%d]" % (stPEventInfo.EventName.decode('UTF-8'),
63  stPEventInfo.nEventID, nBlockId, nTimestamp))
64 CALL_BACK_FUN = EventInfoCallBack(event_callback)
65 
66 
67 if __name__ == "__main__":
68 
69  try:
70  # Initialize SDK resources
71  MvCamera.MV_CC_Initialize()
72 
73  SDKVersion = MvCamera.MV_CC_GetSDKVersion()
74  print ("SDKVersion[0x%x]" % SDKVersion)
75 
76  deviceList = MV_CC_DEVICE_INFO_LIST()
77  tlayerType = (MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_CAMERALINK_DEVICE
78  | MV_GENTL_CXP_DEVICE | MV_GENTL_XOF_DEVICE)
79 
80  # Enumerate devices
81  ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
82  if ret != 0:
83  print ("enum devices fail! ret[0x%x]" % ret)
84  sys.exit()
85 
86  if deviceList.nDeviceNum == 0:
87  print ("find no device!")
88  sys.exit()
89 
90  print ("find %d devices!" % deviceList.nDeviceNum)
91 
92  for i in range(0, deviceList.nDeviceNum):
93  mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
94  if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE:
95  print ("\ngige device: [%d]" % i)
96  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
97  print ("device model name: %s" % strModeName)
98 
99  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chSerialNumber)
100  print("device serial number: %s" % strSerialNumber)
101 
102  nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
103  nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
104  nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
105  nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
106  print ("current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
107  elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
108  print ("\nu3v device: [%d]" % i)
109  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
110  print ("device model name: %s" % strModeName)
111 
112  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber)
113  print ("device serial number: %s" % strSerialNumber)
114  elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE:
115  print ("\nCML device: [%d]" % i)
116  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName)
117  print ("device model name: %s" % strModeName)
118 
119  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber)
120  print ("device serial number: %s" % strSerialNumber)
121  elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE:
122  print ("\nCXP device: [%d]" % i)
123  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName)
124  print ("device model name: %s" % strModeName)
125 
126  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber)
127  print ("device serial number: %s" % strSerialNumber)
128  elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE:
129  print ("\nXoF device: [%d]" % i)
130  strModeName = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName)
131  print ("device model name: %s" % strModeName)
132 
133  strSerialNumber = decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber)
134  print ("device serial number: %s" % strSerialNumber)
135 
136  nConnectionNum = input_func("please input the number of the device to connect:")
137 
138  if int(nConnectionNum) >= deviceList.nDeviceNum:
139  print ("intput error!")
140  sys.exit()
141 
142  # Create the camera instance
143  cam = MvCamera()
144 
145  # Select a device, and create a handle
146  stDeviceList = cast(deviceList.pDeviceInfo[int(nConnectionNum)], POINTER(MV_CC_DEVICE_INFO)).contents
147 
148  ret = cam.MV_CC_CreateHandle(stDeviceList)
149  if ret != 0:
150  raise Exception ("create handle fail! ret[0x%x]" % ret)
151 
152  # Turn on the device
153  ret = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
154  if ret != 0:
155  raise Exception ("open device fail! ret[0x%x]" % ret)
156 
157  # Get optimal packet size (only supported by GigE devices)
158  if stDeviceList.nTLayerType == MV_GIGE_DEVICE or stDeviceList.nTLayerType == MV_GENTL_GIGE_DEVICE:
159  nPacketSize = cam.MV_CC_GetOptimalPacketSize()
160  if int(nPacketSize) > 0:
161  ret = cam.MV_CC_SetIntValue("GevSCPSPacketSize",nPacketSize)
162  if ret != 0:
163  print ("Warning: Set Packet Size fail! ret[0x%x]" % ret)
164  else:
165  print ("Warning: Get Packet Size fail! ret[0x%x]" % nPacketSize)
166 
167  # Set trigger mode to off
168  ret = cam.MV_CC_SetEnumValue("TriggerMode", MV_TRIGGER_MODE_OFF)
169  if ret != 0:
170  raise Exception ("set trigger mode fail! ret[0x%x]" % ret)
171 
172  # Enable Event by setting event selector as ExposureEnd
173  ret = cam.MV_CC_EventNotificationOn("ExposureEnd")
174  if ret != 0:
175  raise Exception ("Set Event Notification On fail! ret[0x%x]" % ret)
176 
177  # Register an event callback
178  ret = cam.MV_CC_RegisterEventCallBackEx("ExposureEnd", CALL_BACK_FUN,None)
179  if ret != 0:
180  raise Exception ("register event callback fail! ret [0x%x]" % ret)
181 
182  # Start grabbing images
183  ret = cam.MV_CC_StartGrabbing()
184  if ret != 0:
185  raise Exception ("start grabbing fail! ret[0x%x]" % ret)
186 
187  print ("press Enter key to stop grabbing.")
188  input_func()
189 
190  # Stop grabbing images
191  ret = cam.MV_CC_StopGrabbing()
192  if ret != 0:
193  raise Exception ("stop grabbing fail! ret[0x%x]" % ret)
194 
195  # Turn off the device
196  ret = cam.MV_CC_CloseDevice()
197  if ret != 0:
198  raise Exception ("close deivce fail! ret[0x%x]" % ret)
199 
200  # Destroy the handle
201  cam.MV_CC_DestroyHandle()
202 
203  except Exception as e:
204  print(e)
205  cam.MV_CC_CloseDevice()
206  cam.MV_CC_DestroyHandle()
207  finally:
208  # Release SDK resources
209  MvCamera.MV_CC_Finalize()